package com.huiruan.config;

/**
 * @author CTL
 * @create 2025-05-10 17:01
 */
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.huiruan.utils.JwtUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

@Component
public class AutoFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 创建时间（仅插入）
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());

        // 更新时间（插入时也需要）
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        // 更新人
        this.strictInsertFill(metaObject, "createBy", String.class, JwtUtil.getUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时间（每次更新刷新）
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        // 更新人
        this.strictUpdateFill(metaObject, "updateBy", String.class, JwtUtil.getUserId());
    }

}
